package net.huashitong.synthetical.newSupervision.dao;

import com.sirdc.modules.core.dao.GenericQuery;
import com.sirdc.modules.core.dao.StringDao;
import com.sirdc.modules.utils.ObjectUtils;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.synthetical.newSupervision.entity.TaskCopeTo;
import net.huashitong.synthetical.newSupervision.entity.TaskNew;
import net.huashitong.synthetical.newSupervision.filter.TaskCopeToFilter;
import net.huashitong.synthetical.newSupervision.filter.TaskNewFilter;
import org.hibernate.SQLQuery;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: ling
 * Date: 2019/8/5
 * Time: 10:12
 * To change this template use File | Settings | File Templates.
 * Description:
 */
@Repository
public class TaskCopeToDao extends StringDao<TaskCopeTo> {

    /**
     * 抄送任务列表
     * @param filter
     * @return
     */
    public List<TaskNew> queryFilter(TaskNewFilter filter) {
        StringBuilder sql = new StringBuilder();
        sql.append("select * from t_task_new t1,t_task_cope_to t2 where t1.sys_id=t2.task_id and t2.county_leader_id = "+filter.getCountyLeaderId());

        // 设置查询总条数,用于分页
        int resultSize = this.countEndorseList(filter);
        filter.getPaging().setRecords(resultSize);
        //设置排序
        if (ObjectUtils.isNotBlank(filter.getPaging())) {
            if (StringUtils.isBlank(filter.getPaging().getSortCol())) {
                filter.getPaging().setSortCol("t1.sys_id");
                filter.getPaging().setSortVal("desc");
            }
            sql.append(" order by "+filter.getPaging().getSortCol()+" "+filter.getPaging().getSortVal());
        }
        SQLQuery query = getSession().createSQLQuery(sql.toString());
        query.setFirstResult(filter.getPaging().getPageSize() * (filter.getPaging().getCurrentPage() - 1));
        query.setMaxResults(filter.getPaging().getPageSize());
        query.setResultTransformer(Transformers.aliasToBean(TaskNew.class));
        List list = query.list();
        return query.list();
    }

    public int countEndorseList(TaskNewFilter filter){
        StringBuilder sql = new StringBuilder();
        sql.append("select count(t1.sys_id) from t_task_new t1,t_task_cope_to t2 where t1.sys_id=t2.task_id and t2.county_leader_id = "+filter.getCountyLeaderId());
        SQLQuery query = getSession().createSQLQuery(sql.toString());
        return ((Number) query.uniqueResult()).intValue();
    }

    public List<TaskCopeTo> getByTaskId(String taskId) {
        GenericQuery query = createQuery("obj");
        query.append(" and taskId=:taskId").setParam("taskId", taskId);
        return query.listResult();
    }
}
